package com.itheima.info.manage.sysytem.controller;

import com.itheima.info.manage.sysytem.domain.Teacher;
import com.itheima.info.manage.sysytem.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    TeacherService teacherService=new TeacherService();
    Scanner sc=new Scanner(System.in);
    public void start() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到学生管理系统---------");
            System.out.println("1:添加老师");
            System.out.println("2:删除老师");
            System.out.println("3:修改老师");
            System.out.println("4:查看老师");
            System.out.println("5:退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findallTeacher();
                    break;
                case "5":
                    System.out.println("返回主界面");
                    return;
            }
        }
    }

    private void updateTeacher() {
        String sid;
        Teacher []teachers=TeacherService.findallTeacher();
        if (teachers==null){
            System.out.println("目前没有老师请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入你要修改的工号");
            sid = sc.next();
            boolean exists = TeacherService.exists(sid);
            if (!exists) {
                System.out.println("没有工号，请先添加");
            } else {break;

            }
        }

        System.out.println("请输入新的老师姓名");
        String newname =sc.next();
        System.out.println("请输入新的老师年龄");
        int newage=sc.nextInt();
        System.out.println("请输入新的老师生日");
        String newbirthday=sc.next();
        Teacher teacher=new Teacher(sid,newname,newage,newbirthday);
        TeacherService.updateTeacher(sid,teacher);
        System.out.println("修改成功");

    }

    private void deleteTeacher() {
        String sid;
        Teacher []teachers=TeacherService.findallTeacher();
        if (teachers==null){
            System.out.println("目前没有老师请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入你要删除的工号");
            sid = sc.next();
            boolean exists = TeacherService.exists(sid);
            if (!exists) {
                System.out.println("没有工号，请先添加");
            } else {break;

            }
        }
        TeacherService.deletaTeacher(sid);
        System.out.println("删除成功");

        
    }

    private void findallTeacher() {
        Teacher []teachers=TeacherService.findallTeacher();
        System.out.println("工号\t姓名\t年龄\t生日");
        for (int i = 0; i <teachers.length ; i++) {
            Teacher teacher=teachers[i];
            if (teacher!=null){
                System.out.println(teacher.getSid()+"\t"+teacher.getName()+"\t"+teacher.getAge()+"\t"+teacher.getBirthday());
            }
            
        }
    }

    public void addTeacher() {
        String sid;
        while (true) {
            System.out.println("请输入老师工号");
            sid = sc.next();
            boolean exists = TeacherService.exists(sid);
            if (exists) {
                System.out.println("已有工号，添加失败");
            } else {
                break;
            }
        }
        System.out.println("请输入老师姓名");
        String name=sc.next();
        System.out.println("请输入老师年龄");
        int age=sc.nextInt();
        System.out.println("请输入老师生日");
        String birthday=sc.next();
        Teacher teacher=new Teacher(sid,name,age,birthday);
        boolean flag=TeacherService.addTeacher(teacher);
        if (flag){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }


    }
}
